1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Collection;
22  import java.util.Set;
23  
24  import javax.annotation.Nullable;
25  
26  /**
27   * An empty immutable set.
28   *
29   * @author Kevin Bourrillion
30   */
31  @GwtCompatible(serializable = true, emulated = true)
32  final class EmptyImmutableSet extends ImmutableSet<Object> {
33    static final EmptyImmutableSet INSTANCE = new EmptyImmutableSet();
34  
35    private EmptyImmutableSet() {}
36  
37    @Override
38    public int size() {
39      return 0;
40    }
41  
42    @Override public boolean isEmpty() {
43      return true;
44    }
45  
46    @Override public boolean contains(@Nullable Object target) {
47      return false;
48    }
49  
50    @Override public boolean containsAll(Collection<?> targets) {
51      return targets.isEmpty();
52    }
53  
54    @Override public UnmodifiableIterator<Object> iterator() {
55      return Iterators.emptyIterator();
56    }
57  
58    @Override boolean isPartialView() {
59      return false;
60    }
61  
62    @Override
63    int copyIntoArray(Object[] dst, int offset) {
64      return offset;
65    }
66  
67    @Override
68    public ImmutableList<Object> asList() {
69      return ImmutableList.of();
70    }
71  
72    @Override public boolean equals(@Nullable Object object) {
73      if (object instanceof Set) {
74        Set<?> that = (Set<?>) object;
75        return that.isEmpty();
76      }
77      return false;
78    }
79  
80    @Override public final int hashCode() {
81      return 0;
82    }
83  
84    @Override boolean isHashCodeFast() {
85      return true;
86    }
87  
88    @Override public String toString() {
89      return "[]";
90    }
91  
92    Object readResolve() {
93      return INSTANCE; // preserve singleton property
94    }
95  
96    private static final long serialVersionUID = 0;
97  }